<!-----------账号登录页面------------->
<template xmlns:v-model="http://www.w3.org/1999/xhtml">
  <div>
    <topheader :toptitle="toptitle"/>
    <div class="wrapper">
      <div class="wrapper-inner">
        <div class="xy-logo">
          <i class="icon icon-xylogo"></i>
        </div>
        <div class="go-login">
          <div class="login-item xy-username">
            <input type="text" v-model:value="xyUser" placeholder="请输入账号/手机号/邮箱"/>
          </div>
          <div class="xy-pwd">
            <input type="password" v-model:value="userpwd" placeholder="请输入密码"/>
            <span class="forget-pwd" @click="forgetPwd">忘记密码</span>
          </div>
          <div class="btn btn-normalsize btn-bluebg btn-xylogin" @click="login">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Vue from "vue"
  import topheader from "@/components/topheader"
  import Message from "iview/src/components/message"
  import apiConfig from "../config/api.js"
  import md5 from "js-md5"

  Vue.prototype.$Message = Message
  export default {
    name: "xylogin",
    components: {
      "topheader": topheader
    },
    data() {
      return {
        toptitle: "XY账号登录",
        xyUser: "",
        userpwd: ""
      }
    },
    methods: {
      login: function () {
      	//    /^0?1[3|4|5|8|7|9][0-9]\d{8}$/
      	//   /^\d{11}$/
        if (this.xyUser === "") {
          this.$Message.warning("请输入账号/手机号/邮箱")
          return
        } else if (this.xyUser !== "" && !/^0?1[3|4|5|8|7|9][0-9]\d{8}$/.test(this.xyUser) && !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(this.xyUser) && !/^[A-Za-z0-9]+$/.test(this.xyUser)) {
          this.$Message.warning("请输入正确的账号/手机号/邮箱")
          return
        } else if (this.userpwd === "") {
          this.$Message.warning("请输入密码")
          return
        } else if (this.userpwd !== "" && !/^[A-Za-z0-9]{6,12}$/.test(this.userpwd)) {
          this.$Message.warning("密码格式不正确，请输入正确密码")
          return
        } else {
          let params = {
            appid: userInfoKit.appid,
            account: this.xyUser,
            password: md5(this.userpwd)
          }
          // 请求XY帐号登录接口
          this.$http(
            {
              method: "GET",
              url: apiConfig.app.user.xyLogin,
              params: params,
              emulateJSON: true
            }).then(function (resp) {
            if (resp.body.code == "200") {
              //保存用户信息
              userInfoKit.save(resp.body)
              // 登录成功后跳转到账户页面
              this.$router.push('/userswitch');
            } else {
              this.$Message.warning(resp.body.msg)
            }
          })
        }
      },
      forgetPwd: function () {
        if (this.xyUser === "") {
          this.$Message.warning("请输入账号/手机号/邮箱")
          return
        }
        if (this.xyUser !== "" && !/^0?1[3|4|5|8|7|9][0-9]\d{8}$/.test(this.xyUser) && !/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(this.xyUser) && !/^[A-Za-z0-9]+$/.test(this.xyUser)) {
          this.$Message.warning("请输入正确的账号/手机号/邮箱")
          return
        }
        let params = {
          appid: userInfoKit.appid,
          account: this.xyUser
        }
        //找回密码（发送验证码接口）
        this.$http({
          method: "GET",
          url: apiConfig.app.user.findPwdByAccount,
          params: params,
          emulateJSON: true
        }).then(function (resp) {
          if (resp.body.code == "200") {
            // 跳转到找回密码页面
            let mobile = resp.body.data.mobile
            this.$router.push({name: "findbackpwd", params: {mobile: mobile, account: this.xyUser}});
          } else if (resp.body.code == "404") {
            let $this = this
            $this.$Message.warning(resp.body.msg)
            setTimeout(function () {
              // 跳转到找回密码（未绑定手机号）页面
              $this.$router.push("/findbackpwdnobindphone")
            }, 800)
          }else{
          		this.$Message.warning(resp.body.msg)
          }
        })
      }
    }
  }
</script>
